<template>
  <div class="app-container">
    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline" style="margin-bottom: -10px">
      <el-form-item>
        <el-input v-model="courseQuery.title" placeholder="课程名称" />
      </el-form-item>

      <el-form-item>
        <el-select
          v-model="courseQuery.status"
          clearable
          placeholder="课程状态"
        >
          <el-option value="Normal" label="已发布" />
          <el-option value="Draft" label="未发布" />
        </el-select>
      </el-form-item>

      <el-form-item label="添加时间">
        <el-date-picker
          v-model="courseQuery.begin"
          type="datetime"
          placeholder="选择开始时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="courseQuery.end"
          type="datetime"
          placeholder="选择截止时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="getCourseList()"
      >查询</el-button
      >
      <el-button type="default" icon="el-icon-delete" @click="resetData()"
      >清空</el-button
      >
    </el-form>
    <el-button type="success" size="mini" icon="el-icon-plus" style="margin-top: -15px;margin-bottom: 5px" @click="openAddCourse">添加课程</el-button>
    <!-- 表格 -->
    <el-table :data="list" height="450" border fit highlight-current-row>
      <el-table-column label="序号" width="70" align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        prop="cover"
        label="课程封面"
        width="320"
        align="center"
        style=""
      >
        <template slot-scope="scope">
          <img :src="scope.row.cover" alt="" style="" class="avatar" />
        </template>
      </el-table-column>
      <el-table-column
        prop="title"
        label="课程名称"
        width="150"
        align="center"
      />

      <el-table-column label="课程状态" width="80" align="center">

        <template slot-scope="scope">
          <el-tag size="small" type="success" v-if="scope.row.status==='Normal'"> 已发布</el-tag>

          <el-tag size="small" v-if="scope.row.status!=='Normal'"> 未发布</el-tag>
        </template>

      </el-table-column>
      <el-table-column
        prop="price"
        label="课程价格"
        width="80"
        align="center"
      />
      <el-table-column
        prop="lessonNum"
        label="课时数"
        width="80"
        align="center"
      />
      <el-table-column
        prop="viewCount"
        label="浏览数量"
        width="80"
        align="center"
      />
      <el-table-column
        prop="gmtCreate"
        label="添加时间"
        width="160"
        align="center"
      />

      <el-table-column
        prop="gmtModified"
        label="更新时间"
        width="160"
        align="center"
      />
      <el-table-column label="操作" width="200" fixed="right" align="center">
        <template slot-scope="scope">
            <el-button type="primary" @click="openEditCourse(scope.row.id)" size="mini" icon="el-icon-edit">修改</el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="removeDataById(scope.row.id)"
            slot="reference"
          >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center"
      layout="total, prev, pager, next, jumper"
      @current-change="getCourseList"
    />
<!--    修改课程-->
    <el-dialog :visible.sync="dialogCourseFormVisible" title="修改课程">
      <el-form :model="oneCourse" label-width="120px">
        <el-form-item label="课程名称">
          <el-input v-model="oneCourse.title" />
        </el-form-item>
        <el-form-item label="课程状态">
          <el-radio-group v-model="oneCourse.status">
            <el-radio label="Normal">已发布</el-radio>
            <el-radio label="Draft">未发布</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="课程价格">
          <el-input-number v-model="oneCourse.price" :precision="2" :step="0.1" :min="0"></el-input-number>
        </el-form-item>
        <el-form-item label="课时数">
          <el-input-number
            v-model="oneCourse.lessonNum"
            :min="0"
            controls-position="right"
          />
        </el-form-item>
        <el-form-item label="课程封面">
          <el-upload
            class="avatar-uploader"
            :action="BASE_API + '/oss/file'"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="oneCourse.cover" :src="oneCourse.cover" class="avatarupdate" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogCourseFormVisible = false">取 消</el-button>
        <el-button
          :disabled="saveVideoBtnDisabled"
          type="primary"
          @click="UpdateOneCourse"
        >确 定</el-button
        >
      </div>
    </el-dialog>
<!--    添加课程-->
    <el-dialog :visible.sync="dialogAddCourse" title="添加课程">
      <el-form :model="addCourse" label-width="120px">
        <el-form-item label="课程名称">
          <el-input v-model="addCourse.title" />
        </el-form-item>
        <el-form-item label="授课教师"><el-select v-model="addCourse.teacherId" placeholder="请选择">
          <el-option
            v-for="teacher in teachers"
            :key="teacher.id"
            :label="teacher.name"
            :value="teacher.id"
          >
          </el-option>
        </el-select>
        </el-form-item>

        <el-form-item label="课程分类">
          <el-select v-model="addCourse.subjectParentId" placeholder="请选择" @change="getSubjectChild(addCourse.subjectParentId)">
            <el-option
              v-for="parent in subParent"
              :key="parent.id"
              :label="parent.title"
              :value="parent.id"
            >
            </el-option>
          </el-select>
          <el-select v-model="addCourse.subjectId" placeholder="请选择">
            <el-option
              v-for="child in subChild"
              :key="child.id"
              :label="child.title"
              :value="child.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="课程状态">
          <el-radio-group v-model="addCourse.status">
            <el-radio label="Normal">已发布</el-radio>
            <el-radio label="Draft">未发布</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="课程价格">
          <el-input-number v-model="addCourse.price" :precision="2" :step="0.1" :min="0"></el-input-number>
        </el-form-item>
        <el-form-item label="课时数">
          <el-input-number
            v-model="addCourse.lessonNum"
            :min="0"
            controls-position="right"
          />
        </el-form-item>
        <el-form-item label="课程封面">
          <el-upload
            class="avatar-uploader"
            :action="BASE_API + '/oss/file'"
            :show-file-list="false"
            :on-success="handleAddAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="addCourse.cover" v-model="addCourse.cover" :src="addCourse.cover" class="avatarupdate" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogAddCourse = false">取 消</el-button>
        <el-button
          :disabled="saveVideoBtnDisabled"
          type="primary"
          @click="AddCourse"
        >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<!--所有课程列表-->
<script>
import course from "@/api/course";
import teacher from '../../api/teacher'
import subject from '../../api/subject'
export default {

  data() {
    //定义变量和初始值
    return {
      saveVideoBtnDisabled:false,
      list: null, //查询之后返回的集合
      page: 1,
      total: 0,
      limit: 10,
      courseQuery: {
        status: null,
      },
      oneCourse:{

      },
      subParent:{},
      subChild:{},
      teachers:{},
      addCourse:{
        subjectId:"",
        subjectParentId:"",
        cover:""
      },
      dialogAddCourse:false,
      dialogCourseFormVisible: false, //章节弹框
      BASE_API: process.env.BASE_API, // 接口API地址
      loading: true,
    };
  },
  created() {
    this.getCourseList();
  },
  methods:{
    getSubject(){
      subject.getParent().then(response=>{
        this.subParent=response.data.parent;
      })
    },
    getSubjectChild(id){
      this.addCourse.subjectId=null;
      subject.getChild(id).then(response=>{
        this.subChild=response.data.child;
      })
    },
    getTeacher(){
      teacher.getTeacher().then(response=>{
        this.teachers=response.data.teacher;
      })
    },
    openAddCourse(){
      this.dialogAddCourse=true;
      this.getTeacher();
      this.getSubject();
    },
    getCourseList(page=1){
      this.page=page;
      course.getCoursePageCondition(this.page,this.limit,this.courseQuery).then(response=>{
        this.list=response.data.page.records;
        this.total = response.data.page.total;
      }).catch((error) => {
        console.log(error);
      });
    },
     removeDataById(id){
      this.$confirm("此操作将永久删除课程记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        //点击确定，执行then方法
        //调用删除的方法
        course.deleteCourse(id).then(response=>{
          //删除成功
          //提示信息
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          //回到列表页面
          this.getCourseList();
        });
      }); //点击取消，执行catch方法
    },
    resetData() {
      //清空
      this.courseQuery = {};
      this.getCourseList();
    },
    //弹出修改课程页面
    openEditCourse(courseId) {
      //弹框
      this.dialogCourseFormVisible = true;
      course.getOneCourse(courseId).then(response=>{
        this.oneCourse=response.data.course;
      })
    },
    handleAvatarSuccess(res, file) {
      this.oneCourse.cover =res.data.url;
      //   this.cover = URL.createObjectURL(file.raw);
    },
    handleAddAvatarSuccess(res, file) {
      this.addCourse.cover =res.data.url;
      //   this.cover = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {

      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isLt2M;
    },
    AddCourse(){
      course.addCourse(this.addCourse).then(response=>{
        this.dialogAddCourse = false;
        this.$message({
          type: "success",
          message: "添加课程成功!",
        });
        this.getCourseList();
      })
    },
    UpdateOneCourse(){
      course.updateCourse(this.oneCourse).then(response=>{
        this.dialogCourseFormVisible = false;
        this.$message({
          type: "success",
          message: "修改课程信息成功!",
        });
        this.getCourseList();
      })
    }
  }
}
</script>

<style>
.avatar {
  width: 180px;
  height: 102px;
  display: block;
  margin-left: 22%;
}
body {
  margin: 0;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 140px;
  height: 140px;
  line-height: 140px;
  text-align: center;
}
.avatarupdate{
  width: 265.5px;
  height: 150px;
  display: block;
}
</style>
